package edu.map.aamas.charts;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DLtd;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Vector;

import javax.swing.JPanel;

import edu.map.aamas.rnn.RNN;
import edu.map.aamas.rnn.RnnListener;

public abstract class RnnChart extends JPanel implements RnnListener {

	private static final long serialVersionUID = 1L;
	private Vector<ITrace2D> traces = new Vector<ITrace2D>(); 
	private RNN rnn;
	private int curColor = 0;

	public Color getRandomColor() {

		Color[] colors = new Color[] {
				Color.blue,
				Color.red,
				new Color(0,170,0),								
				Color.orange,
				Color.black,
				new Color(255,0,255)
		};

		return colors[ curColor++ % colors.length ];
	}
	
	public RnnChart(RNN rnr, String[] traceNames) {
		this.rnn = rnr;
		for (String name : traceNames) {
			ITrace2D trace = new Trace2DLtd(100, name);			
			trace.setColor(getRandomColor());
			traces.add(trace);			
			chart.addTrace(trace);
		}
		this.setLayout(new BorderLayout());
		this.add(chart);
	}
	
	public void update(double[] values) {
		if (values.length == traces.size()) {
			for (int i = 0; i < values.length; i++)
				traces.get(i).addPoint(rnn.iteration, values[i]);
		}
	}

	private Chart2D chart = new Chart2D(){
		private static final long serialVersionUID = 1L;
		public synchronized void paint(java.awt.Graphics arg0) {
			((Graphics2D)arg0).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			super.paint(arg0);			
		}
	};
	
	public void rnnRestarted(RNN rnr) {
		for (ITrace2D trace : traces)
			trace.removeAllPoints();
	}
}
